<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="org.example.pojo.Product" %>
<%@ page import="java.util.List" %>

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>商品管理 - MobileWorld</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f8f9fa;
        }
        header {
            background-color: #0078D7;
            color: white;
            text-align: center;
            padding: 1.5rem;
            position: relative;
            animation: fadeInDown 0.8s ease-out;
        }
        header h1 {
            margin: 0;
        }
        .navigate-buttons {
            position: absolute;
            top: 50%;
            right: 20px;
            transform: translateY(-50%);
            display: flex;
            gap: 10px;
        }
        .navigate-buttons a {
            background-color: #ffffff;
            color: #0078D7;
            border: 2px solid #0078D7;
            border-radius: 5px;
            padding: 0.5rem 1rem;
            font-size: 0.9rem;
            text-decoration: none;
            font-weight: bold;
            transition: all 0.3s ease;
            text-align: center;
        }
        .navigate-buttons a:hover {
            background-color: #0078D7;
            color: white;
        }
        .container {
            max-width: 800px;
            margin: 20px auto;
            padding: 1.5rem;
            background: #fff;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            animation: fadeInUp 0.8s ease-out;
        }
        .message {
            padding: 10px;
            margin-bottom: 20px;
            border-radius: 5px;
            font-size: 0.9rem;
        }
        .message.success {
            background-color: #d4edda;
            color: #155724;
        }
        .message.error {
            background-color: #f8d7da;
            color: #721c24;
        }
        h2 {
            color: #0078D7;
            margin-bottom: 1rem;
        }
        form {
            display: flex;
            flex-direction: column;
            gap: 1rem;
            margin-bottom: 2rem;
        }
        label {
            font-weight: bold;
        }
        input[type="text"], input[type="number"], button {
            padding: 0.8rem;
            font-size: 1rem;
            border: 1px solid #ddd;
            border-radius: 5px;
            outline: none;
        }
        button {
            background-color: #0078D7;
            color: white;
            cursor: pointer;
            border: none;
            border-radius: 5px;
            padding: 0.8rem;
            font-size: 1rem;
            transition: background-color 0.3s ease;
        }
        button:hover {
            background-color: #005bb5;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 1rem;
        }
        table th, table td {
            text-align: center;
            padding: 10px;
            border: 1px solid #ddd;
        }
        table th {
            background-color: #f4f4f4;
            font-weight: bold;
        }
        table tr:hover {
            background-color: #f9f9f9;
        }
        .actions {
            display: flex;
            gap: 5px;
            justify-content: center;
        }
        footer {
            text-align: center;
            margin-top: 20px;
            font-size: 0.9rem;
            color: #555;
        }
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        @keyframes fadeInDown {
            from {
                opacity: 0;
                transform: translateY(-20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
    </style>
</head>
<body>
<header>
    <h1>商品管理</h1>
    <div class="navigate-buttons">
        <a href="products?action=add">添加商品</a> <!-- 添加商品按钮 -->
        <a href="orders">进入订单页面</a>
        <a href="index.jsp">返回首页</a>
    </div>
</header>
<div class="container">
    <% if (request.getAttribute("message") != null) { %>
    <div class="message success"><%= request.getAttribute("message") %></div>
    <% } %>
    <% if (request.getAttribute("error") != null) { %>
    <div class="message error"><%= request.getAttribute("error") %></div>
    <% } %>
    <h2>商品列表</h2>
    <table>
        <tr>
            <th>ID</th>
            <th>名称</th>
            <th>价格</th>
            <th>库存</th>
            <th>操作</th>
        </tr>
        <%
            List<Product> products = (List<Product>) request.getAttribute("productList");
            if (products != null && !products.isEmpty()) {
                for (Product product : products) {
        %>
        <tr>
            <td><%= product.getId() %></td>
            <td><%= product.getName() %></td>
            <td>￥<%= product.getPrice() %></td>
            <td><%= product.getStock() %></td>
            <td class="actions">
                <form action="products" method="post" style="display:inline;">
                    <input type="hidden" name="action" value="delete">
                    <input type="hidden" name="productId" value="<%= product.getId() %>">
                    <button type="submit" onclick="return confirm('确定删除商品吗？')">删除</button>
                </form>
                <form action="editProduct.jsp" method="get" style="display:inline;">
                    <input type="hidden" name="productId" value="<%= product.getId() %>">
                    <button type="submit">编辑</button>
                </form>
                <form action="products" method="post" style="display:inline;">
                    <input type="hidden" name="action" value="order">
                    <input type="hidden" name="productId" value="<%= product.getId() %>">
                    <input type="number" name="quantity" placeholder="数量" min="1" max="<%= product.getStock() %>" required>
                    <button type="submit">下单</button>
                </form>
            </td>
        </tr>
        <%
            }
        } else {
        %>
        <tr><td colspan="5">暂无商品</td></tr>
        <%
            }
        %>
    </table>
</div>
<footer>
    &copy; 2024 MobileWorld. 一站式手机购物平台.
</footer>
</body>
</html>
